package com.extremelylargepackage.entity;

import com.extremelylargepackage.GameResources;
import com.extremelylargepackage.sprite.AbstractSprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;

public class Button extends Entity 
{
	private String _text;
	private Rect _textSize;
	private float _textLeft;
	private float _textTop;
	private Paint _paint;
	private ButtonPressListener _onPress;
	private int _highlightColor;
	private int _textColor = Color.WHITE;
	
	public Button(AbstractSprite s, float x, float y) 
	{
		super();
		_sprite = s;
		_locX = x;
		_locY = y;
		initPaint();
	}
	
	public Button(float x, float y) 
	{	
		super();
		_locX = x;
		_locY = y;
		initPaint();
	}
	
	private void initPaint()
	{
		_paint = new Paint();
		_paint.setColor(_textColor);
		_paint.setTypeface(Typeface.DEFAULT_BOLD);
		_paint.setStyle(Style.STROKE);
		_paint.setTextSize(GameResources.getLevelFontSize());
		_paint.setShadowLayer(5, 0, 0, Color.BLACK);
		_paint.setAntiAlias(true);
		
		_highlightColor = _paint.getColor();
	}
	
	public void setTextSize(int size)
	{
		_paint.setTextSize(size);
		setText(_text);
	}

	@Override
	public void updateState(long time) 
	{ 
		move(time);
		updateTextLocation();
	}

	@Override
	public void onCollision(Entity z) { }

	@Override
	public void draw(Canvas c) 
	{
		if(_sprite != null)
			c.drawBitmap(_sprite.getBitmap(), _locX, _locY, null);
		
		//WARNING, the origin for drawing text is the bottom left, wtf android?
		c.drawText(_text, _textLeft, _textTop + _textSize.height(), _paint);
		Paint white = new Paint();
		
		//Use this code for debugging the locations of the button.
//		white.setColor(Color.WHITE);
//		c.drawRect(getLocX(), getLocY(), getLocX() + 5, getLocY() + 5, white);
//		c.drawRect(getLocX() + getWidth(), getLocY(), getLocX() + getWidth() + 5, getLocY() + 5, white);
//		c.drawRect(getLocX(), getLocY() + getHeight(), getLocX() + 5, getLocY() + getHeight() + 5, white);
//		c.drawRect(getLocX() + getWidth(), getLocY() + getHeight(), getLocX() + getWidth() + 5, getLocY() + getHeight() + 5, white);	
	}
	
	public void setText(String text)
	{
		_text = text;
		_textSize = new Rect(); 
		_paint.getTextBounds(_text, 0, _text.length(), _textSize);
		updateTextLocation();
	}
	
	private void updateTextLocation()
	{
		if(_sprite != null)
		{
			_textLeft = (_locX + _sprite.getWidth() / 2) - (_textSize.width() / 2); 
			_textTop = (_locY + _sprite.getHeight() / 2) - (_textSize.height() / 2);
		}
		else
		{
			_textLeft = _locX - (_textSize.width() / 2);
			_textTop = _locY - (_textSize.height() / 2);
		}
	}
	
	public String getText()
	{
		return _text;
	}
	
	@Override
	public float getLocX()
	{
		if(_textLeft < _locX)
			return _textLeft;
		else
			return _locX;
	}
	
	@Override
	public float getLocY()
	{
		if(_textTop < _locY)
			return _textTop;
		else
			return _locY;
	}
	
	@Override
	public int getWidth()
	{
		if(_sprite == null || _textSize.width() > _sprite.getWidth())
			return _textSize.width();
		else
			return _sprite.getWidth();
	}
	
	@Override
	public int getHeight()
	{
		if(_sprite == null)
			return _textSize.height();
		return _sprite.getHeight();
	}
	
	private boolean spriteContaintsPoint(float x, float y)
	{
		return x > _locX && x < _locX + _sprite.getWidth() && y > _locY && y < _locY + _sprite.getHeight();
	}
	
	public float getBottom()
	{
		return getLocY() + getHeight();
	}
	
	public float getRight()
	{
		return getLocX() + getWidth();
	}
	
	public boolean containsPoint(float x, float y)
	{
		//return _textSize.contains((int)x, (int)y) || (_sprite != null && spriteContaintsPoint(x, y));
		boolean left = x > getLocX() ;
		boolean top = y > getLocY();
		boolean right = x < getLocX() + getWidth();
		boolean bottom = y < getLocY() + getHeight();
		return left && top && right && bottom;
		//return x > getLocX() && y > getLocY() && x < getLocX() + getWidth() && y < getLocY() + getHeight();
	}
	
	public void press()
	{
		if(_onPress != null)
		{
			_onPress.onClick();
		}
	}
	
	public void setButtonPressListener(ButtonPressListener bpl)
	{
		_onPress = bpl;
	}
	
	public interface ButtonPressListener
	{
		public void onClick();
	}

	public void setHighlight(int color)
	{
		_highlightColor = color;
	}
	
	public void hightlight() 
	{
		_paint.setColor(_highlightColor);
	}

	public void unHightlight() 
	{
		_paint.setColor(_textColor);
	}
	
}
